
import java.io.File;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.EventObject;
import java.util.Random;
import java.util.Scanner;

import javax.swing.JComboBox;
import javax.swing.JFileChooser;


import javax.swing.JOptionPane;

import javazoom.jlgui.basicplayer.*;


public class SoundManager extends EventObject {
	
	private static BasicPlayer player = new BasicPlayer();
	private static final long serialVersionUID = -5252739881599532532L;
	private static int count = 0;
    private static ArrayList<String> playSong = new ArrayList<String>();	
    private static float vol = 50/100f;
    private String userName;
    private JComboBox<String> box;
    private JFileChooser chooser;
    private static String songPath;
	
	public SoundManager(Object source,String eventName) {
		super(source);
		chooser = new JFileChooser();
        chooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
        
		if (eventName.equals("playMp3"))  playMp3(songPath);
		if (eventName.equals("stopMp3")) stopMp3();
		if (eventName.equals("pauseMp3")) pauseMp3();
		if(eventName.equals("playSelectedSong")) playSelectedSong();
		

	}
	public SoundManager(Object source,String eventName, String path) {
		super(source);
		
		if (eventName.equals("playMp3"))  playMp3(path);
	}
	public SoundManager(Object source,String eventName, float volume){
		super(source);
		if (eventName.equals("volumeMp3")) volumeMp3(volume);
	}

	
	public SoundManager(Object source,String eventName, String name, JComboBox com){
		super(source);
		
		userName = name;
		box = com;
		chooser = new JFileChooser();
        chooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
        
		if (eventName.equals("addSound")) addSound();
		if (eventName.equals("changeSound")) changeSound();
		if (eventName.equals("fillSound")) fillSound();
		
		
	}

	private void addSound(){
		int returnVal = chooser.showOpenDialog(null);
        if(returnVal == JFileChooser.APPROVE_OPTION) {
	        DatabaseManager.openConnection();
			Statement stmt = null;
			String sql;
			try {
				String name = doubleBackSlash(chooser.getSelectedFile().getPath());
				String songName = chooser.getSelectedFile().getName();
				sql = "insert into flappybirdv2db.songpath (songPath, userName, songName) values ('"+name+"','"+userName+"', '" + songName + "')";
				stmt = DatabaseManager.conn.createStatement();
				stmt.executeUpdate(sql);
				
		        box.addItem(songName);
		        playSong.add(name);
				
			} catch (SQLException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			DatabaseManager.closeConnection();     
        }
	}
	
	@SuppressWarnings("unused")
	private void changeSound(){
		DatabaseManager.openConnection();
		Statement stmt = null;
		String sql;
		try {
			System.out.print(box.getSelectedItem().toString());
			sql = "Select * from flappybirdv2db.songpath where userName = '"+userName+"' and songName = '"+box.getSelectedItem().toString() +"'";
			stmt = DatabaseManager.conn.createStatement();
			ResultSet rs = stmt.executeQuery(sql);
			
			while(rs.next()){
				songPath = rs.getString("songPath");
			}
			if(false)
				JOptionPane.showMessageDialog(null, "Wrong Path!", "ERROR", JOptionPane.ERROR_MESSAGE);	
			sql = "update flappybirdv2db.songpath set dateadded = CURRENT_TIMESTAMP where userName = '"+userName+"' and songName = '"+box.getSelectedItem().toString() +"'";
			stmt = DatabaseManager.conn.createStatement();
			stmt.executeUpdate(sql);

		} catch (SQLException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		DatabaseManager.closeConnection();
		playMp3(songPath);
	}
	
	private void fillSound(){
		DatabaseManager.openConnection();
		Statement stmt = null;
		try {
			String sql = "Select * from flappybirdv2db.songpath where userName = '"+ userName +"'";
			stmt = DatabaseManager.conn.createStatement();
			ResultSet rs = stmt.executeQuery(sql);
			
			while(rs.next()){
				box.addItem(rs.getString("songName"));
			}
		} catch (SQLException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		DatabaseManager.closeConnection();
	}
	//Playing Sound	
	private void playMp3(String songPath)
    {   		

		File file = new File(songPath);
		
		try {
			player.open(file);
			player.play();
			player.setGain(vol);
		} catch (BasicPlayerException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}

    }

    // Stopping the sound
	
	private void stopMp3()
    {   
		try {
			player.stop();
		} catch (BasicPlayerException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
			
    }
	
	// Pause && Resume
	
	private void pauseMp3()
    {   
		if (count == 0)
			playMp3(songPath);
		else if (count % 2 != 0){
			try {
				player.pause();
			} catch (BasicPlayerException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		} 
		else if (count % 2 == 0){
			try {
				player.resume();
			} catch (BasicPlayerException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		count++;
    }
	
	private void volumeMp3(float volume)
    {   
		vol = volume;
        try {
        	player.setGain(vol);
		} catch (BasicPlayerException e1) {
     		// TODO Auto-generated catch block
			e1.printStackTrace();
		}
    }

	private String doubleBackSlash(String str){
		String returnStr = str.replace("\\", "\\\\");
		return returnStr;
	}
	
	private void playSelectedSong(){
		int returnVal = chooser.showOpenDialog(null);
        if(returnVal == JFileChooser.APPROVE_OPTION) {
        	String name = doubleBackSlash(chooser.getSelectedFile().getPath());
			playMp3(name); 
        }
	}

}
